﻿using Caliburn.Micro;
using KouDai.WinPad.Helper;
using KouDai.WinPad.Models;
using KouDai.WinPad.Services;
using Newtonsoft.Json;
using NextEngine.HttpEngine;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.UI.Xaml.Controls;

namespace KouDai.WinPad.ViewModels
{
    public class SecondHotPageViewModel : KouDaiScreen
    {
        #region Fields
        private INavigationService _navigationService;
        private ObservableCollection<GroupCollection<HoneyEntity>> _secondHoneyItems  = new ObservableCollection<GroupCollection<HoneyEntity>>();
        private string _title =  string.Empty;
        private BindableCollection<ZoomOutModel> _snapListItems = new BindableCollection<ZoomOutModel>();
        
        #endregion

        #region Constructor

        public SecondHotPageViewModel()
        {
            if (DesignMode.DesignModeEnabled)
            {
                LoadDesignData();
            }
        }

        public SecondHotPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            _navigationService = navigationService;
        }

        #endregion

        #region Properties

        public ObservableCollection<GroupCollection<HoneyEntity>> SecondHoneyItems
        {
            get { return _secondHoneyItems; }
            set
            {
                if (value != _secondHoneyItems)
                {
                    _secondHoneyItems = value;
                    this.NotifyOfPropertyChange(() => this.SecondHoneyItems);
                }
            }
        }

        public BindableCollection<ZoomOutModel> SnapListItems
        {
            get { return _snapListItems; }
            set { _snapListItems = value; }
        }


        public override bool Loading
        {
            get
            {
                return base.Loading;
            }
            set
            {
                if (value != base._loading)
                {
                    base._loading = value;
                    BusyInicator.Instance.IsBusy = value;
                    this.NotifyOfPropertyChange(() => this.Loading);
                }
            }
        }

        public string Title
        {
            get { return _title; }
            set
            {
                if (value != _title)
                {
                    _title = value;
                    this.NotifyOfPropertyChange(() => this.Title);
                }
            }
        }

        public List<HotEntity> FatherHotItems { get; set; }
        public HotEntity CurrentHotItem { get; set; }

        public string HotItemJson { get; set; }
        public string FatherItemJson { get; set; }

        #endregion

        #region Override

        public override void LoadDesignData()
        {
            #region DesignTitle
            this.Title = "少女装";
            #endregion

            #region ZoomInDesignData

            ZoomOutModel model = new ZoomOutModel() { GroupName = "最新推荐", ImageUrl = "ms-appx:///Assets/Images/child_recommend.png" };
            SnapListItems.Add(model);
            GroupCollection<HoneyEntity> collection = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList = new List<HoneyEntity>();
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15866793897_146x219.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img.geilicdn.com/taobao19765708787_146x194.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848113020_146x146.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848325228_146x219.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.03, ImageUrlForIphone = "http://img.geilicdn.com/taobao19779004225_146x150.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.57, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865634518_146x229.jpg", RowSpan = 1, ColSpan = 1 });
            foreach (HoneyEntity item in honeyList)
            {
                collection.Add(item);
            }
            SecondHoneyItems.Add(collection);

            model = new ZoomOutModel() { GroupName = "婴儿", ImageUrl = "ms-appx:///Assets/Images/child_baby.png" };
            SnapListItems.Add(model);
            GroupCollection<HoneyEntity>  collection1 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity>  honeyList1 = new List<HoneyEntity>();
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", RowSpan = 1, ColSpan = 1 });
            foreach (HoneyEntity item in honeyList1)
            {
                collection1.Add(item);
            }
            SecondHoneyItems.Add(collection1);

            model = new ZoomOutModel() { GroupName = "男童", ImageUrl = "ms-appx:///Assets/Images/child_boybaby.png" };
            SnapListItems.Add(model);
            GroupCollection<HoneyEntity>  collection2 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList2 = new List<HoneyEntity>();
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", RowSpan = 1, ColSpan = 1 });
            foreach (HoneyEntity item in honeyList2)
            {
                collection2.Add(item);
            }
            SecondHoneyItems.Add(collection2);

            model = new ZoomOutModel() { GroupName = "女童", ImageUrl = "ms-appx:///Assets/Images/child_girlbaby.png" };
            SnapListItems.Add(model);
            GroupCollection<HoneyEntity>  collection3 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity>  honeyList3 = new List<HoneyEntity>();
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", RowSpan = 1, ColSpan = 1 });
            foreach (HoneyEntity item in honeyList3)
            {
                collection3.Add(item);
            }
            SecondHoneyItems.Add(collection3);

            model = new ZoomOutModel() { GroupName = "男孩", ImageUrl = "ms-appx:///Assets/Images/child_boy.png" };
            SnapListItems.Add(model);
            GroupCollection<HoneyEntity> collection4 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList4 = new List<HoneyEntity>();
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", RowSpan = 1, ColSpan = 1 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", RowSpan = 1, ColSpan = 1 });
            foreach (HoneyEntity item in honeyList4)
            {
                collection4.Add(item);
            }
            SecondHoneyItems.Add(collection4);

            #endregion
        }

        protected override void OnViewAttached(object view, object context)
        {
            base.OnViewAttached(view, context);
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            LoadData();
            LoadSecondGroupData();
        }

        private void LoadData()
        {
            FatherHotItems = JsonConvert.DeserializeObject<List<HotEntity>>(this.FatherItemJson);
            CurrentHotItem = JsonConvert.DeserializeObject<HotEntity>(this.HotItemJson);
        }
        #endregion

        #region Method
        public async void LoadSecondGroupData()
        {
            this.Title = CurrentHotItem.OriginalAppName;
            List<HotEntity> subHotItems = CurrentHotItem.SubCateories;
            if (subHotItems == null)
            {
                throw new ArgumentNullException("subHotItems");
            }
            Loading = true;
            foreach (HotEntity subHotItem in subHotItems)
            {
                await LoadSingleData(CurrentHotItem.OriginalAppName, subHotItem.OriginalAppName, subHotItem.CombineAppId);
            }
            Loading = false;
        }

        private async Task LoadSingleData(string name,string subName, string token)
        {
            #region ZoomOutModel
            ZoomOutModel model = null;

            #region Girl
            if (Title.Contains("少女装"))
            {
                if (subName.Equals("最新推荐"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/girl_recommend.png" };
                }
                else if (subName.Equals("靓衣"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/girl_cloth.png" };
                }
                else if (subName.Equals("美鞋"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/girl_shoe.png" };
                }
                else if (subName.Equals("潮包"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/girl_bag.png" };
                }
                else if (subName.Equals("配饰"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/girl_play.png" };
                }
                else if (subName.Equals("家居"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/girl_jiaju.png" };
                }
            }
            #endregion

            #region Lady
            if (Title.Contains("轻熟女"))
            {
                if (subName.Equals("最新推荐"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/lady_recommend.png" };
                }
                else if (subName.Equals("鞋履"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/lady_shoe.png" };
                }
                else if (subName.Equals("服饰"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/lady_cloth.png" };
                }
                else if (subName.Equals("包袋"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/lady_bag.png" };
                }
                else if (subName.Equals("配饰"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/lady_play.png" };
                }
                else if (subName.Equals("美容"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/lady_meirong.png" };
                }
            }
            #endregion

            #region Man
            if (Title.Contains("男人装"))
            {
                if (subName.Equals("最新推荐"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/man_recommend.png" };
                }
                else if (subName.Equals("鞋展"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/man_shoe.png" };
                }
                else if (subName.Equals("IN包"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/man_bag.png" };
                }
                else if (subName.Equals("潮服"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/man_cloth.png" };
                }
                else if (subName.Equals("玩物"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/man_play.png" };
                }
                else if (subName.Equals("3c"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/man_3c.png" };
                }

            }
            #endregion

            #region Child
            if (Title.Contains("童装"))
            {
                if (subName.Equals("最新推荐"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/child_girlbaby.png" };
                }
                else if (subName.Equals("婴儿"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/child_baby.png" };
                }
                else if (subName.Equals("男童"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/child_boybaby.png" };
                }
                else if (subName.Equals("女童"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/child_girlbaby.png" };
                }
                else if (subName.Equals("男孩"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/child_boy.png" };
                }
                else if (subName.Equals("女孩"))
                {
                    model = new ZoomOutModel() { GroupName = subName, ImageUrl = "ms-appx:///Assets/Images/child_girl.png" };
                }
            }

            #endregion


            if (model == null)
            {
                return;
            }
            #endregion

            GroupCollection<HoneyEntity> collection = new GroupCollection<HoneyEntity>(model);
            BackData backData = await ServiceManager.GetImageWall(token, 0, 20);
            if (backData.IsSuccess)
            {
                List<HoneyEntity> honeyList = (List<HoneyEntity>)backData.ResposeData;
                model.SubImageUrlOne = honeyList[0].ImageUrlForIphone;
                model.Price1 = honeyList[0].Price;
                model.SubImageUrlTwo = honeyList[1].ImageUrlForIphone;
                model.Price2 = honeyList[1].Price;
                SnapListItems.Add(model);
                for (int i = 0; i < 6; i++)
                {
                    HoneyEntity item = honeyList[i];
                    if (honeyList.IndexOf(item) == 0)
                    {
                        item.ColSpan = 2;
                        item.RowSpan = 2;
                    }
                    else
                    {
                        item.ColSpan = 1;
                        item.ColSpan = 1;
                    }
                    collection.Add(item);
                }
            }
            else
            {
                Debug.WriteLine("backdata-->" + backData.Msg.ToString());
            }
            SecondHoneyItems.Add(collection);
        }
        #endregion

        #region UIMethod

        public void ItemSelect(ItemClickEventArgs args)
        {
            HoneyEntity selectedItem = (HoneyEntity)args.ClickedItem;
            //TODO navigate to the detail
        }

        public void ItemTitleClick(object obj)
        {
            GroupCollection<HoneyEntity> group = (GroupCollection<HoneyEntity>)obj;
            var groupName = ((ZoomOutModel)group.Key).GroupName;
            GroupTitleClick(groupName);
        }

        public void ListItemClick(ItemClickEventArgs args)
        {
            if (args.ClickedItem is ZoomOutModel)
            {
                ZoomOutModel model = (ZoomOutModel)args.ClickedItem;
                string groupName = model.GroupName;
                GroupTitleClick(groupName);
            }
        }

        public void GroupTitleClick(string groupName)
        {
            if (groupName.Equals(CurrentHotItem.OriginalAppName))
            {
                groupName = "最新推荐";
            }
            HotEntity selectedItem = CurrentHotItem.SubCateories.Where(x => x.OriginalAppName.Equals(groupName)).FirstOrDefault();
            if (selectedItem != null)
            {
               _navigationService.UriFor<MainPageViewModel>().
               WithParam<string>(x => x.DisplayName, selectedItem.OriginalAppName)
               .WithParam<string>(x => x.CurrentHotItemJson, JsonConvert.SerializeObject(selectedItem))
               .WithParam<string>(x => x.FatherHotItemJson, JsonConvert.SerializeObject(CurrentHotItem))
               .WithParam<string>(x => x.ID, selectedItem.CombineAppId).Navigate();
            }
        }

        public void SameLevelNavigate(string groupName)
        {
            HotEntity hotItem = FatherHotItems.Where(x => x.OriginalAppName.Equals(groupName.Trim())).FirstOrDefault();
            if (hotItem != null)
            {
                if (hotItem.SubCateories.Count > 0)
                {
                    string json = JsonConvert.SerializeObject(hotItem);
                    _navigationService.UriFor<SecondHotPageViewModel>()
                        .WithParam<string>(x => x.HotItemJson, json)
                        .WithParam<string>(x => x.FatherItemJson, FatherItemJson)
                        .Navigate();
                }
                else
                {
                    _navigationService.UriFor<MainPageViewModel>()
                   .WithParam<string>(x => x.CurrentHotItemJson, JsonConvert.SerializeObject(hotItem))
                   .WithParam(x => x.DisplayName, groupName.Trim())
                   .WithParam<string>(x => x.ID, hotItem.CombineAppId)
                   .Navigate();
                }
                FrameStackHelper.Instance.AddCountStack();
            }
        }

        #endregion
    }
}
